package com.example.testrecette;



import java.util.ArrayList;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.util.Log;

public class BDAcces {

	
	
	
	private static final String RECETTE_TABLE = "recette_larecette";
	
	private static final String INGREDIENT_RECETTE_TABLE = "recette_contenu";

	private static final String INGREDIENT_TABLE = "recette_condiment";
	
	private static final String DATABASE_NAME = "recette_android";
	
	private final Context mCtx;

	private DatabaseHelper mDbHelper;
	private SQLiteDatabase mDb;
	private SQLiteQueryBuilder mbbuilder;
	
	private static final String TAG = "BDAcces";
	private static final int DATABASE_VERSION = 1;
	//les differentes requete pour le creation des tables
	private static final String CREATE_RECETTE_TABLE ="CREATE TABLE recette_larecette ("
								  +"id_larecette INTEGER PRIMARY KEY  AUTOINCREMENT,"
								  +"id_user INTEGER(11)  NULL,"
								  +"id_pays INTEGER   NULL,"
								  +"id_typerepas INTEGER   NULL,"
								  +"nom_larecette VARCHAR(250)  NULL,"
								  +"supplogique_larecette VARCHAR(10)  NULL,"
								  +"recette_larecette LONGTEXT  NULL,"
								  +"nbrpers_larecette INTEGER   NULL,"
								  +"dateenreg_larecette DATETIME  NULL,"
								  +"nbrcalories_larecette INTEGER   NULL,"
								  +"liensite_enreg LONGTEXT  NULL"
								  +")";
	
	private static final String CREATE_RECETTE_CONTENU ="CREATE TABLE recette_contenu ("
								  +"id_contenu INTEGER PRIMARY KEY AUTOINCREMENT,"
								  +"id_unitemesure INT NOT NULL,"
								  +"id_larecette INTEGER NOT NULL,"
								  +"id_condiment INTEGER NOT NULL,"
								  +"nombre_contenu DECIMAL NOT NULL,"
								  +"unitemesure_contenu VARCHAR(20) NULL"
								  +")";
	
	private static final String CREATE_RECETTE_CONDIMENT ="CREATE TABLE recette_condiment ("
								+"id_condiment INTEGER PRIMARY KEY AUTOINCREMENT,"
								+"id_typealiment INTEGER UNSIGNED NOT NULL,"
								+"nom_condiment VARCHAR(250) NOT NULL,"
								+"mesure_condiment VARCHAR(100) NOT NULL,"
								+"supplogique_condiment VARCHAR(10) NOT NULL"
								+")";
	
	
	
	public BDAcces(Context ctx) {
	 
		 this.mCtx = ctx;
	 }
 
	 
private static class DatabaseHelper extends SQLiteOpenHelper {
		
		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}
		
		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(CREATE_RECETTE_TABLE);
			db.execSQL(CREATE_RECETTE_CONTENU);
			db.execSQL(CREATE_RECETTE_CONDIMENT);
	    }
		
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
			+ newVersion + ", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS "+INGREDIENT_TABLE);
			db.execSQL("DROP TABLE IF EXISTS "+RECETTE_TABLE);
			db.execSQL("DROP TABLE IF EXISTS "+INGREDIENT_RECETTE_TABLE);
			onCreate(db);
			}
		}
    

    //---opens the database---
      public void open() throws SQLException 
      {
    	  	mDbHelper = new DatabaseHelper(mCtx);
  			mDb = mDbHelper.getWritableDatabase();
  			//mbbuilder = 
  			
      }

      //---closes the database---    
      public void close() 
      {
    	  mDbHelper.close();
      }
      public String renvoi_requete(){
  		return CREATE_RECETTE_TABLE;
  		}
      public String Liste_table(){

    	  return mDb.getPath();
    	}
      
      public long insertRecette(String sNomRecette, String sContenuRecette) 
      {
          ContentValues initialValues = new ContentValues();
          initialValues.put("nom_larecette", sNomRecette);
          initialValues.put("recette_larecette", sContenuRecette);
          return mDb.insert(RECETTE_TABLE, null, initialValues);
      }
    
      
      //renvoi liste recette
      public ArrayList<String> renvoi_liste_recette(){
    	  
    	  String[] aTableCollonne=new String[] {"nom_larecette"};
		  Cursor objCursor = mDb.query(RECETTE_TABLE,aTableCollonne,null,null,null,null,null,null);
		  int iPostNomchaine = objCursor.getColumnIndex("nom_larecette");
		  int itotal = objCursor.getCount();
		  ArrayList<String> aTableRetour = new ArrayList<String>();
		  objCursor.moveToFirst();
		/* Check if our result was valid. */
             if (objCursor != null) {            	 
            	 if (objCursor.isFirst()) { 
            		 int i = 0; 
            		 do {
                         i++;
                         String resultsring = objCursor.getString(iPostNomchaine);
                         aTableRetour.add(resultsring);
                         objCursor.moveToNext();
            	     }while(!objCursor.isLast());
             }
             }
			 			 
		  return aTableRetour;
      }
      
      public void update_bdd(){
  		int newversion = mDb.getVersion()+1;
  		mDbHelper.onUpgrade(mDb, mDb.getVersion(), newversion);	
  		//mDbHelper.onCreate(mDb);
  	  }

	

	
//**************************fin de la classe**********************************	
}
